package net.xo68.destiny.core.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间工具类
 * @author wuxie
 * @version 2020-10-23
 */
public final class TimeUtils {
    private TimeUtils() {
        throw new IllegalArgumentException();
    }

    public final static String DATETIME_FORMAT_LOCAL_DATE_TIME="yyyy-MM-dd HH:mm:ss";
    /**
     * 一天的毫秒数
     */
    public final static Long DAY_MILLI=1000*60*60*24l;

    /**
     * 取当前时间
     * @return
     */
    public static Date nowDate(){
        return new Date();
    }
    /**
     * 取当前时间
     * @return
     */
    public static LocalDateTime nowLocalDateTime(){
        return LocalDateTime.now();
    }
    /**
     * 取当前时间
     * @return
     */
    public static LocalDate nowLocalDate(){
        return LocalDate.now();
    }
    public static long nowMilli(){
        //return toMilliTime(LocalDateTime.now());
        return System.currentTimeMillis();
    }

    /**
     * 总天数
     * @return
     */
    public static long nowDay(){
        //return toMilliTime(LocalDateTime.now());
        return toDayTime(nowLocalDateTime());
    }
    /**
     * 当前时间字符串格式
     * @return
     */
    public static String nowString(){
        return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }
    /**
     * 当前时间字符串格式
     * @param dateTimeFormatter  如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String nowString( String dateTimeFormatter){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateTimeFormatter));
    }
    public static String nowString( DateTimeFormatter dateTimeFormatter){
        return LocalDateTime.now().format(dateTimeFormatter);
    }




    /**
     * 转换为字符串
     * @param date
     * @param dateTimeFormatter
     * @return
     */
    public static String toString(Date date, String dateTimeFormatter){
        return ofLocalDateTime(date).format(DateTimeFormatter.ofPattern(dateTimeFormatter));
    }
    public static String toString(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter){

        return localDateTime.format(dateTimeFormatter);
    }
    public static String toString(LocalDateTime localDateTime, String dateTimeFormatter){
        return localDateTime.format(DateTimeFormatter.ofPattern(dateTimeFormatter));
    }
    public static String toString(LocalDate localDate, DateTimeFormatter dateTimeFormatter){
        return localDate.format(dateTimeFormatter);
    }
    public static String toString(LocalDate localDate, String dateTimeFormatter){
        return localDate.format(DateTimeFormatter.ofPattern(dateTimeFormatter));
    }

    public static Date ofDate(LocalDateTime localDateTime){
        return ofDate(localDateTime, ZoneId.systemDefault());
    }
    public static Date ofDate(long milliTime){
        return ofDate(LocalDateTimeUtils.parseDateTime(milliTime));
    }
    public static Date ofDate(LocalDateTime localDateTime, ZoneId zoneId){
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }

    /**
     * 字符串转换为 LocalDateTime
     * @param strDateTime
     * @param dateTimeFormatter
     * @return
     */
    public static LocalDateTime ofLocalDateTime(String strDateTime, DateTimeFormatter dateTimeFormatter){
        return LocalDateTime.parse(strDateTime,dateTimeFormatter);
    }
    public static LocalDateTime ofLocalDateTime(String strDateTime, String dateTimeFormatter){
        return LocalDateTime.parse(strDateTime,DateTimeFormatter.ofPattern(dateTimeFormatter));
    }

    /**
     * Date LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime ofLocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId  = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }
    /**
     * 毫秒long 转换为 localdatetime
     * @param milliTime
     * @return
     */
    public static LocalDateTime ofLocalDateTime(long milliTime){
        return ofLocalDateTime(milliTime,ZoneId.systemDefault());
    }
    public static LocalDateTime ofLocalDateTime(long milliTime, ZoneId zoneId){
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliTime),zoneId);
    }

    public static LocalDate ofLocalDate(String strDate, DateTimeFormatter dateTimeFormatter){
        return LocalDate.parse(strDate,dateTimeFormatter);
    }
    public static LocalDate ofLocalDate(String strDate, String dateTimeFormatter){
        return LocalDate.parse(strDate,DateTimeFormatter.ofPattern(dateTimeFormatter));
    }
    public static LocalDate ofLocalDate(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId  = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }



    /**
     * 取得毫秒 long
     * @param localDateTime
     * @return
     */
    public static long toMilliTime(LocalDateTime localDateTime){
        return toMilliTime(localDateTime,ZoneId.systemDefault());
    }
    public static long toMilliTime(LocalDateTime localDateTime, ZoneId zoneId){
        return localDateTime.atZone(zoneId).toInstant().toEpochMilli();
    }
    public static long toDayTime(LocalDateTime localDateTime){
        return toMilliTime(localDateTime)/DAY_MILLI;
    }
    public static long toDayTime(LocalDateTime localDateTime, ZoneId zoneId){
        return toMilliTime(localDateTime,ZoneId.systemDefault())/DAY_MILLI;
    }

    /**
     * 长时间时间间隔计算
     * @param startTime
     * @param endTime
     * @return
     */
    public static Duration calculateTimeSpan(LocalDateTime startTime, LocalDateTime endTime){
        return Duration.between(startTime, endTime);
    }
    public static Duration calculateTimeSpan(Date startTime, Date endTime){
        return Duration.between(ofLocalDateTime(startTime), ofLocalDateTime(endTime));
    }

    /**
     * 短时间时间间隔计算
     * @param startTime
     * @param endTime
     * @return
     */
    public static Period calculateTimeSpan(LocalDate startTime, LocalDate endTime){
        return Period.between(startTime, endTime);
    }
}
